package team.hyznrj.studentsys.service.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import team.hyznrj.studentsys.dao.mapper.StudentMsgMapMapper;
import team.hyznrj.studentsys.entity.DataReport;
import team.hyznrj.studentsys.entity.ExcelEntity;
import team.hyznrj.studentsys.entity.ExcelUnitObject;
import team.hyznrj.studentsys.entity.StudentMsgMap;
import team.hyznrj.studentsys.entity.StudentMsgMapExample;
import team.hyznrj.studentsys.enums.DataReportEnum;
import team.hyznrj.studentsys.exception.AuthenticationOnFlagException;
import team.hyznrj.studentsys.exception.DataBaseObjectNull;
import team.hyznrj.studentsys.exception.DataBaseObjectRefuse;
import team.hyznrj.studentsys.exception.DataBaseOperationError;
import team.hyznrj.studentsys.exception.DataIllegalException;
import team.hyznrj.studentsys.exception.DataNullException;
import team.hyznrj.studentsys.service.ExcelService;
import team.hyznrj.studentsys.service.StudentMsgMapService;
import team.hyznrj.studentsys.utils.ValueUtil;
import team.hyznrj.studentsys.utils.WriteExcel;

@Service
public class StudentMsgMapServiceImpl implements StudentMsgMapService {
	private final static String DEFAULT_FLAG="-1";
	@Autowired
	StudentMsgMapMapper studentMsgMapper;
	
	@Override
	public DataReport addMsg(String value, boolean necessary,String flag) throws DataNullException, DataBaseOperationError, AuthenticationOnFlagException {
		/* 默认状态下，新插入的字段是可删除的 */
		if (StringUtils.isEmpty(value)) {
			throw new DataNullException("插入值不能为空");
		}
		/**
		 * 获取学院,按学院插入字段
		 * */
		String format = ValueUtil.getFlag(flag, 1);
		StudentMsgMapExample example=new StudentMsgMapExample();
		example.createCriteria().andFlagEqualTo(format).andValueEqualTo(value);
		List<StudentMsgMap> selectByExample = studentMsgMapper.selectByExample(example);
		if(selectByExample!=null&&selectByExample.size()>0){
			throw new DataBaseOperationError("字段 "+value+" 重复", DataReportEnum.INSERT_ERROR);
		}
		int result = studentMsgMapper.insert(new StudentMsgMap(ObjectId.get().toString(), value, true, necessary,format));	
		if(result<=0){
			throw new DataBaseOperationError("插入失败", DataReportEnum.INSERT_ERROR);
		}
		DataReport data = new DataReport(DataReportEnum.SUCCESS,"插入成功", null,true);
		return data;
	}

	@Override
	public DataReport updateMsg(String id, String value, boolean necessary,String flag) throws DataNullException, DataIllegalException, DataBaseObjectNull, DataBaseOperationError, DataBaseObjectRefuse, AuthenticationOnFlagException {
		DataReport dataReport=new DataReport();
		/* 异常流 */
		if (StringUtils.isEmpty(value)) {
			throw new DataNullException("关键值不能为空");
		}
		/* 异常流 */
		if (StringUtils.isEmpty(id)) {
			throw new DataIllegalException("id为无效值");
		}
		/**
		 * 按学院标志段分割
		 */
		String format = ValueUtil.getFlag(flag, 1);
		StudentMsgMapExample example=new StudentMsgMapExample();
		example.createCriteria().andFlagEqualTo(format).andValueEqualTo(value);
		List<StudentMsgMap> selectByExample = studentMsgMapper.selectByExample(example);
		if(selectByExample!=null&&selectByExample.size()>0){
			for(StudentMsgMap map:selectByExample){
				if(!map.getId().equals(id)){
					throw new DataBaseOperationError("字段 "+value+" 重复", DataReportEnum.INSERT_ERROR);
				}
			}
		}
		StudentMsgMap msg = studentMsgMapper.selectByPrimaryKey(id);
		/* 异常流 */
		if (msg == null) {
			throw new DataBaseObjectNull("更新对象为空");
		}else{
			/* 异常流判断 */
			if (msg.getDeleteAble()) {
				/* 主要代码 */
				if (dataReport.isFlag()) {
					msg.setValue(value);
					msg.setNecessary(necessary);
					int result = studentMsgMapper.updateByPrimaryKey(msg);
					if (result > 0) {
						dataReport.setCode(DataReportEnum.SUCCESS);
						dataReport.setReport("更新成功");
						dataReport.setFlag(true);
					} else {
						throw new DataBaseOperationError("更新失败", DataReportEnum.UPDATE_ERROR);
					}
				}
			} else {
				/* 异常流 */
				throw new DataBaseObjectRefuse("该对象不能被修改");
			}
		}
		return dataReport;
	}

	@Override
	public DataReport deleteMsg(String id) throws DataBaseObjectNull, DataBaseOperationError, DataBaseObjectRefuse {
		DataReport dataReport=new DataReport();
		StudentMsgMap msg = null;
		if (!StringUtils.isEmpty(id)) {
			msg = studentMsgMapper.selectByPrimaryKey(id);
		}
		if (msg == null) {
			throw new DataBaseObjectNull("删除的对象不存在");
		}
		if (msg.getDeleteAble()) {
			int result = studentMsgMapper.deleteByPrimaryKey(id);
			if (result > 0) {
				dataReport.setCode(DataReportEnum.SUCCESS);
				dataReport.setReport("删除成功");
				dataReport.setFlag(true);
			} else {
				throw new DataBaseOperationError("删除失败", DataReportEnum.DELETE_ERROR);
			}
		} else {
			throw new DataBaseObjectRefuse("该对象不能被删除");
		}
		return dataReport;
	}
	private List<StudentMsgMap> getAllMsgData(String flag){
		String format =ValueUtil.getFlag(flag, 1);
		StudentMsgMapExample condition =new StudentMsgMapExample();
		/**
		 * 条件: -1值和学院值
		 */
		condition.or(condition.createCriteria().andFlagEqualTo(DEFAULT_FLAG));
		condition.or(condition.createCriteria().andFlagEqualTo(format));
		List<StudentMsgMap> data = studentMsgMapper.selectByExample(condition);
		if (data == null || data.size() == 0) {
			throw new DataBaseObjectNull("对象为空");
		}
		return data;
	}
	
	@Override
	public DataReport getAllMsg(String flag) throws DataBaseObjectNull, DataNullException, AuthenticationOnFlagException {
		DataReport dataReport=new DataReport();
		dataReport.setFlag(true);
		dataReport.setCode(DataReportEnum.SUCCESS);
		dataReport.setData(getAllMsgData(flag));
		return dataReport;
	}
	@Override
	public DataReport getMsgById(String id) throws DataIllegalException, DataBaseObjectNull {
		DataReport dataReport=new DataReport();
		StudentMsgMap data=null;
		if(StringUtils.isEmpty(id)){
			throw new DataIllegalException("id不能为空");
		}else{
			data = studentMsgMapper.selectByPrimaryKey(id);
			dataReport.setData(data);
			if (data == null) {
				throw new DataBaseObjectNull("id无效");
			}
			dataReport.setFlag(true);
			dataReport.setCode(DataReportEnum.SUCCESS);
		}
		return dataReport;
	}

	@Override
	public Map<String, String> getAllMsgIdMap(String flag) {
		 Map<String, String> map=new HashMap<>();
		List<StudentMsgMap> allMsgData = getAllMsgData(flag);
		for(StudentMsgMap m:allMsgData){
			map.put(m.getValue(), m.getId());
		}
		return map;
	}
}
